package xiangmu;

import java.util.Scanner;
import java.util.TreeMap;

/**
 * 设计一个：学生管理系统
 * 1.添加学生信息\n 2.显示学生信息\n 3.删除学生信息\n 4.修改学生信息\n5.查看学生信息\n 6.排序\n7.退出
 *
 */

public class Manage {
    // 主模块；
    public static void main(String[] args) {
        Student[] stus = new Student[10];
        Student student = new Student();
        int number = 0;
        int studentCount = 0;
//        int time = 0;
        System.out.println("欢迎进入学生管理系统\n1.添加学生信息\n2.显示学生信息\n" +
                "3.删除学生信息\n4.修改学生信息\n5.查询学生信息\n6.排序\n7.退出");
        while (true) {
            System.out.println("请输入选择：");
            Scanner scanner = new Scanner(System.in);
            number = scanner.nextInt();
            boolean flag;
            switch (number) {
                case 1:
                    add(stus,studentCount);
                    break;
                case 2:
                    display(stus,studentCount);
                    break;
                case 3:
//                     flag = delete(stus,studentCount);
                    break;
                case 4:
                    update(stus,studentCount);
                    break;
                case 5:
                    select(stus,studentCount);
                    break;
                case 6:
//                    Sort(stus,studentCount);
                    break;
                case 7:
                    System.exit(0);
                    break;
                default:
                    System.out.println("请输入1-7之间的整数");
            }
        }
    }

    // 1.添加学生信息块
    public static void add(Student[] stus,int studentCount) {
        if (studentCount >= stus.length) {
            System.out.println("添加失败");
        } else {
            System.out.println("请输入学生学号");
            Scanner s1 = new Scanner(System.in);
            String studentID = s1.next();
            System.out.println("请输入学生姓名");
            Scanner s2 = new Scanner(System.in);
            String studentName = s2.next();
            System.out.println("请输入学生成绩");
            Scanner s3 = new Scanner(System.in);
            double studentScore = s3.nextDouble();
            Student newStud = new Student(studentID, studentName, studentScore);
            studentCount++;
            stus[0] = newStud;
            System.out.println("添加成功");
        }
    }
    // 2.显示学生信息块
    public static void display(Student[] stus,int studentCount) {
        System.out.println("学号"+"\t姓名"+"\t成绩\n");
        for(int i = 0; i < studentCount; i++){
            System.out.println(stus[i].detail());
        }

    }

    // 3.删除学生信息
    public boolean  delete(Student[] stus,int studentCount) {
        select(stus,studentCount);
        System.out.println("是否确定要删除此条信息。是：选1；否：选0");
        Scanner s = new Scanner(System.in);
        int j = s.nextInt();
        switch(j){
            case 0:
                System.out.println("你已取消了删除操作");
                break;
            case 1:
                for(int i=0;i<studentCount;i++){
                    if(stus[i].getName().equals(j)){
                        if(i<studentCount-1){
                            stus[i] = stus[i+1];
                            stus[i-1] = null;
                            studentCount--;
                            return true;
                        }

                    }
                }
        }
        return false;
    }

    // 4.修改学生信息块
    public static void update(Student[] stus,int studentCount) {
           int index = select(stus, studentCount);
            System.out.println("请重新输入学号");
            Scanner s1 = new Scanner(System.in);
            String stuId = s1.next();
            System.out.println("请重新输入姓名");
            Scanner s2 = new Scanner(System.in);
            String stuName = s1.next();
            System.out.println("请重新输入分数");
            Scanner s3 = new Scanner(System.in);
            double stuScore = s3.nextDouble();
            Student stu  = new Student(stuId, stuName, stuScore);
            stus[index] = stu;
            System.out.println("修改完成");


    }

    // 5.查询学生信息块
    public static Integer  select(Student[] stus,int studentCount) {
        System.out.println("请输入学生姓名");
        Scanner s = new Scanner(System.in);
        String str = s.next();
        for (int i = 0; i < studentCount; i++) {
            if (str.equals(stus[i].getName())) {
                System.out.println("学号" + "\t姓名" + "\t成绩");
                System.out.println(stus[i].getId() + "\t" + stus[i].getName() + "\t"
                        + stus[i].getScore());
                return i;
            }
        }
        System.out.println("没有此人");
        return null;
    }





//    // 6.排序块
//    public static void Sort(Integer[] studentId, String[] studentName,
//                            Integer[] studentScore, int studentCount) {
//        System.out.println("请选择排序方法:1.按学号排序\t2.按成绩排序");
//        Scanner s1 = new Scanner(System.in);
//        int number = s1.nextInt();
//        switch (number) {
//            case 1:
//                for (int i = 0; i < studentCount; i++) {
//                    for (int j = 0; j < studentCount - 1 - i; j++) {
//                        if (studentId[j] > studentId[j + 1]) {
//                            int tp = studentId[j];
//                            studentId[j] = studentId[j + 1];
//                            studentId[j + 1] = tp;
//
//                            tp = studentScore[j];
//                            studentScore[j] = studentScore[j + 1];
//                            studentScore[j + 1] = tp;
//
//                            String tp1 = studentName[j];
//                            studentName[j] = studentName[j + 1];
//                            studentName[j + 1] = tp1;
//                        }
//                    }
//                }
//                display(studentId, studentName, studentScore, studentCount);
//                break;
//
//            case 2:
//                for (int i = 0; i < studentCount; i++) {
//                    for (int j = 0; j < studentCount - 1 - i; j++) {
//                        if (studentScore[j] > studentScore[j + 1]) {
//                            int tp = studentScore[j];
//                            studentScore[j] = studentScore[j + 1];
//                            studentScore[j + 1] = tp;
//
//                            tp = studentId[j];
//                            studentId[j] = studentId[j + 1];
//                            studentId[j + 1] = tp;
//
//                            String tp1 = studentName[j];
//                            studentName[j] = studentName[j + 1];
//                            studentName[j + 1] = tp1;
//                        }
//                    }
//                }
//                display(studentId, studentName, studentScore, studentCount);
//                break;
//            default:
//                System.out.println("请输入1或者2");
//        }

//    }
}
